package demon2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.peter.demon_02.Poem;

public class Pommy {

	public static void main(String[] args) {
		
		

		String url = "https://www.gushiwen.org";
		Document doc = null;
		try {
			doc = Jsoup.connect("https://www.gushiwen.org/shiwen/").timeout(10000).get();
			// System.out.println(doc.html());
			Elements eles = doc.select(".titletype #type3 a");

			for (var a : eles) {
				System.out.println(a.text() + "       " + url + a.attr("href"));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Poem> list = sorted("https://www.gushiwen.org/shiwen/default_3A131313131313A1.aspx");

		for (var a : list) {
			System.out.println(a);
		}

	}

	@SuppressWarnings("finally")
	public static List<Poem> sorted(String url) {
		List<Poem> lists = new ArrayList<Poem>();
		int count = 1;
		int sumcount = 99;
		try {
			for (; count <= 20; count++) {
				Document doucment = Jsoup.connect(url).timeout(10000).get();
				sumcount = Integer.valueOf(doucment.select(".pagesright #sumPage").text());
				Elements eles = doucment.select(".cont");
				for (Element ele : eles) {
					for (Element sss : ele.select(".source").prev()) {
						Poem poem = new Poem(sss.text(), sss.select("a").attr("href"));
						lists.add(poem);
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return lists;
		}

	}

}
